package com.haidechizi.fastadmin.modules.job.controller;

import com.haidechizi.fastadmin.common.annotation.OperateLog;
import com.haidechizi.fastadmin.common.page.PageDto;
import com.haidechizi.fastadmin.common.utils.PageInfo;
import com.haidechizi.fastadmin.common.utils.Response;
import com.haidechizi.fastadmin.common.validator.ValidatorUtils;
import com.haidechizi.fastadmin.modules.job.entity.ScheduleJob;
import com.haidechizi.fastadmin.modules.job.service.ScheduleJobService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 定时任务
 */
@RestController
@RequestMapping("/sys/schedule")
public class ScheduleJobController {
    @Autowired
    private ScheduleJobService scheduleJobService;

    /**
     * 定时任务列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:schedule:list")
    public Response list(PageDto pageDto, ScheduleJob scheduleJob) {
        PageInfo page = scheduleJobService.queryPage(pageDto, scheduleJob);
        return Response.ok().put("page", page);
    }

    /**
     * 定时任务信息
     */
    @RequestMapping("/info/{jobId}")
    @RequiresPermissions("sys:schedule:info")
    public Response info(@PathVariable("jobId") Long jobId) {
        ScheduleJob schedule = scheduleJobService.selectById(jobId);
        return Response.ok().put("schedule", schedule);
    }

    /**
     * 保存定时任务
     */
    @OperateLog("保存定时任务")
    @RequestMapping("/save")
    @RequiresPermissions("sys:schedule:save")
    public Response save(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.save(scheduleJob);
        return Response.ok();
    }

    /**
     * 修改定时任务
     */
    @OperateLog("修改定时任务")
    @RequestMapping("/update")
    @RequiresPermissions("sys:schedule:update")
    public Response update(@RequestBody ScheduleJob scheduleJob) {
        ValidatorUtils.validateEntity(scheduleJob);
        scheduleJobService.update(scheduleJob);
        return Response.ok();
    }

    /**
     * 删除定时任务
     */
    @OperateLog("删除定时任务")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:schedule:delete")
    public Response delete(@RequestBody Long[] jobIds) {
        scheduleJobService.deleteBatch(jobIds);
        return Response.ok();
    }

    /**
     * 立即执行任务
     */
    @OperateLog("立即执行任务")
    @RequestMapping("/run")
    @RequiresPermissions("sys:schedule:run")
    public Response run(@RequestBody Long[] jobIds) {
        scheduleJobService.run(jobIds);
        return Response.ok();
    }

    /**
     * 暂停定时任务
     */
    @OperateLog("暂停定时任务")
    @RequestMapping("/pause")
    @RequiresPermissions("sys:schedule:pause")
    public Response pause(@RequestBody Long[] jobIds) {
        scheduleJobService.pause(jobIds);
        return Response.ok();
    }

    /**
     * 恢复定时任务
     */
    @OperateLog("恢复定时任务")
    @RequestMapping("/resume")
    @RequiresPermissions("sys:schedule:resume")
    public Response resume(@RequestBody Long[] jobIds) {
        scheduleJobService.resume(jobIds);
        return Response.ok();
    }

}
